<template>
  <div>
    <div class="num-box">
      <span class="title">手机号码/邮箱：</span>
      <span class="phone">{{ codeForm.userName | userNickFormat }}</span>
    </div>
    <div class="from">
      <el-form label-width="100px" class="demo-ruleForm">
        <el-form-item label="验证码：" prop="code">
          <el-input v-model="code" placeholder="请输入短信验证码" />
          <button class="gain-btn" :disabled="!canSend" @click="getCode" v-text="content" />
          <i class="iconfont icon-key key" />
          <div class="box">
            <div v-show="state" class="error-msg">{{ hintCode }}</div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="content-item">
      <button class="last-step distance" @click="last">上一步</button>
      <button class="btn distance" @click="next">下一步</button>
    </div>
  </div>
</template>
<script>
import { countdown } from '@/util/countdown'
import { forgetPassword, check } from '@/api/reset-password'
export default {
  name: 'SecondRestPWD',
  props: {
    codeForm: {
      required: true,
      type: Object
    }
  },
  data() {
    return {
      content: '获取验证码', // 按钮里显示的内容
      totalTime: 60,
      clock: undefined,
      canSend: true,
      code: undefined,
      hintCode: '请先填写验证码',
      state: false
    }
  },
  methods: {
    login() {
      this.$router.push({
        path: '/login'
      })
    },
    last() {
      this.$emit('last')
    },
    next() {
      if (!this.code) {
        this.hintCode = '请先填写验证码'
        this.state = true
        return
      }
      const headers = {
        uuid: this.uuid || this.codeForm.uuid,
        userName: this.codeForm.userName
      }
      check(headers, this.code).then(res => {
        if (res.data) {
          this.$emit('setPasswordForm', {
            uuid: res.message,
            userName: this.codeForm.userName
          })
          this.$emit('next')
        }
      }).catch(error => {
        this.$message(error.data)
        this.canSend = true
      })
    },
    getCode() {
      if (!this.canSend) {
        return
      }
      countdown(this)
      forgetPassword(this.codeForm.userName).then(res => {
        this.uuid = res.data
      })
    }
  }
}
</script>
<style scoped lang="scss">
.password {
  padding-top: 127px;
  padding-bottom: 87px;
  background: #f5f5f5;
  .password-content {
    background: #ffffff;
    .contents {
      height: 600px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .box {
      }
      .step {
        margin-left: 470px;
        width: 490px;
      }
      .two{
          .num-box{
            margin: 47px 0 16px 366px;
            .title{
              margin-right: 14px;
              margin-left: -12px;
            }
          }
          .from{
            margin-left: 340px;
          }
          .input{
            position: relative;

          }
          .key{
            position: absolute;
            left: 50px;
            font-size: 21px;
            top: -3px;
          }
          .mail{
            position: relative;
          }
          .gain-btn{
            position: absolute;
            width: 92px;
            height: 38px;
            top: 1px;
            background: #F1F1F1;
            left: 337px;
            line-height: 40px;
            text-align: center;
            color: #8E8E93;
            cursor: pointer;
            border-radius: 3px;
          }
          .box{
            height: 20px;
            line-height: 20px;
            .error-msg{
              text-align: left;
              padding-left: 30px;
              color: #f56c6c;
            }
          }
        }
      .content-item {
        margin-top: 0px;
        display: flex;
        flex-direction: column;
        align-items: center;
        .distance {
          margin-left: 140px;
        }
        .last-step {
          background: #ffffff;
          border: 1px solid #c7c7cc;
          width: 400px;
          height: 42px;
          line-height: 42px;
          text-align: center;
          color: #a7a7aa;
          cursor: pointer;
          border-radius: 3px;
          margin-left: 140px;
          margin-bottom: 16px;
          font-size: 18px;
        }
      }
      .btn {
        background: #ff3b30;
        width: 400px;
        height: 42px;
        line-height: 42px;
        text-align: center;
        color: #ffffff;
        cursor: pointer;
        border-radius: 3px;
        margin-left: 130px;
        font-size: 18px;
      }
    }
  }
  /deep/.contents {
    .el-form-item__error {
      margin-left: 30px;
    }
  }
  /deep/ .contents {
    .el-input {
      width: 400px;
      height: 42px;
    }
    .el-form-item__label {
      width: 130px !important;
    }
  }
  /deep/ .steps {
    .el-steps {
      position: absolute;
      width: 1000px;
      top: 40px;
      right: -340px;
    }
  }
  /deep/ .one {
    .el-input__inner {
      text-indent: 40px;
    }
  }
  /deep/ .three-from {
    .el-input__inner {
      text-indent: 40px;
    }
  }
  /deep/ .from {
    .el-input__inner {
      text-indent: 40px;
    }
  }
  /deep/ .content-item {
    .el-input {
      width: 280px;
    }
  }
}
</style>
